/**----------------------------------------
 * @description 图形工具类
 * @author      lxc
 * @file        Tools.ts
 * @Date        2022-05-05
 * @version     1.0.0
 * ----------------------------------------
 */
export class GraphicsTool {
    /**
     * 绘制矩形区域
     * @param rect 
     * @param tag 
     * @param color 
     */
    drawRectArea(rect: cc.Rect, tag: string = "rect", color: cc.Color = cc.Color.RED) {
        if (gVar.TestLayer.active) {
            let node = gVar.TestLayer.getChildByName(tag);
            if (node) {
                let graphics = node.getComponent(cc.Graphics);
                graphics.clear(true);
                graphics.fillRect(0, 0, rect.width, rect.height);
                node.setPosition(rect.x, rect.y);
            } else {
                let node = new cc.Node(tag)
                node.parent = gVar.TestLayer;
                let graphics = node.addComponent(cc.Graphics)
                graphics.fillColor = color;
                graphics.fillColor.setA(120) // 设置填充颜色透明度
                graphics.fillRect(0, 0, rect.width, rect.height);
                node.setPosition(rect.x, rect.y);
            }
        }
    }
    /**
     * 绘制圆形区域
     * @param centerPoint 
     * @param radius 
     * @param tag 
     * @param color 
     */
    drawCircle(centerPoint: cc.Vec2, radius: number = 60, tag: string = "circle", color: cc.Color = cc.Color.GREEN) {
        if (gVar.TestLayer.active) {
            let node = gVar.TestLayer.getChildByName(tag)
            if (node) {
                let graphics = node.getComponent(cc.Graphics)
                graphics.clear(true)
                graphics.circle(centerPoint.x, centerPoint.y, radius);
                // graphics.stroke();
                graphics.fill()
            } else {
                node = new cc.Node(tag)
                node.parent = gVar.TestLayer;
                var graphics = node.addComponent(cc.Graphics)
                graphics.circle(centerPoint.x, centerPoint.y, radius);
                graphics.fillColor = color;
                graphics.fillColor.setA(120);
                // graphics.stroke()
                graphics.fill();
            }
        }
    }
    /**
     * 按顺序绘制点的连线
     * @param points 
     * @param tag 
     * @param color 
     * @param fill 
     */
    drawPoints(points: cc.Vec2[], tag: string = "points", color: cc.Color = cc.Color.RED, fill: boolean = false) {
        if (gVar.TestLayer.active) {
            let node = gVar.TestLayer.getChildByName(tag)
            let drawLine = function (graphics: cc.Graphics) {
                graphics.clear(true)
                for (let i = 0; i < points.length; i++) {
                    if (i == 0) {
                        graphics.moveTo(points[0].x, points[0].y)
                    } else {
                        graphics.moveTo(points[i].x, points[i].y)
                    }
                }
                fill && graphics.lineTo(points[0].x, points[0].y)
                graphics.stroke()
            }
            if (node) {
                let graphics = node.getComponent(cc.Graphics)
                drawLine(graphics);
                fill && graphics.fill()
            } else {
                node = new cc.Node(tag)
                let graphics = node.addComponent(cc.Graphics)
                graphics.fillColor = color
                graphics.fillColor.setA(120)
                graphics.strokeColor = color
                drawLine(graphics)
                fill && graphics.fill()
            }
        }
    }
}